package com.wsg.lightaccounting.PopupWindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.PopupWindow;

import com.wsg.lightaccounting.Activity.AccountActivity;
import com.wsg.lightaccounting.Activity.CashCardActivity;
import com.wsg.lightaccounting.Activity.MainActivity;
import com.wsg.lightaccounting.R;

/**
 * Created by dell-pc on 2016/11/19.
 */

public class MarkPopupWindow extends PopupWindow{
    private View mMenuView;
    private ImageButton add,back;
    private EditText mark;
    private Context context;
    public MarkPopupWindow(final Activity context, int w, int h){
        super(context);
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        mMenuView = inflater.inflate(R.layout.mark_layout, null);
        add = (ImageButton) mMenuView.findViewById(R.id.add);
        back = (ImageButton) mMenuView.findViewById(R.id.back);
        mark = (EditText) mMenuView.findViewById(R.id.add_content);
        mark.setFocusable(true);
        back.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
        this.setContentView(mMenuView);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccountActivity.mark_information = String.valueOf(mark.getText());
                dismiss();
            }
        });
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(w);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(h);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.layout).getTop();
                int y=(int) event.getY();
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(y<height){
                        dismiss();
                    }
                }
                return true;
            }
        });
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        CashCardActivity.changeInput(mark,context,true);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        CashCardActivity.changeInput(mark,context,true);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);
        CashCardActivity.changeInput(mark,context,true);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        CashCardActivity.changeInput(mark,context,true);
    }
    @Override
    public void dismiss() {
        super.dismiss();
        CashCardActivity.changeInput(mark,context,false);
    }
}
